package com.android.bookgarden.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.bookgarden.MainActivity;
import com.android.bookgarden.MyApplication;
import com.android.bookgarden.R;
import com.android.bookgarden.apis.Apis;
import com.android.bookgarden.base.BaseActivity;
import com.android.bookgarden.bean.DataBean;
import com.android.bookgarden.bean.UserInfo;
import com.android.bookgarden.constract.RealtConstract;
import com.android.bookgarden.persenter.BasePersenter;
import com.android.bookgarden.utli.JsonUitl;
import com.android.bookgarden.utli.SpUtils;
import com.android.bookgarden.utli.Utlis;
import com.android.bookgarden.views.LoadingDialog;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Create by zouqh
 * on 2018/11/30
 **/
public class ChangePassActivity extends BaseActivity implements View.OnClickListener,RealtConstract.View {
    @BindView(R.id.close)
    ImageView close;
    @BindView(R.id.title_text)
    TextView titleText;
    @BindView(R.id.oldPass)
    EditText oldPass;
    @BindView(R.id.newPass)
    EditText newPass;
    @BindView(R.id.loinBut)
    TextView loinBut;
    @BindView(R.id.lookPass)
    CheckBox lookPass;
    private RealtConstract.Presenter presenter;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_change_pass;
    }

    @Override
    protected void initView() {
        presenter = new BasePersenter(this,this);
        titleText.setText("修改登录密码");
        close.setOnClickListener(this);
        loinBut.setSelected(false);
        loinBut.setEnabled(false);
        loinBut.setOnClickListener(this);
        oldPass.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                setSubmitStatus();
            }
            @Override
            public void afterTextChanged(Editable s) {
            }
        });
        newPass.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                setSubmitStatus();
            }
            @Override
            public void afterTextChanged(Editable s) {
            }
        });
        lookPass.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {
                // TODO Auto-generated method stub
                if (isChecked) {
                    // 设置成明文
                    // This transformation method causes any carriage return

                    newPass
                            .setTransformationMethod(HideReturnsTransformationMethod
                                    .getInstance());
                } else {
                    // 设置成密文
                    //
                    newPass
                            .setTransformationMethod(PasswordTransformationMethod
                                    .getInstance());
                }
            }
        });
    }


    private void setSubmitStatus() {
        if ( oldPass.getText().toString() != null && !"".equals(oldPass.getText().toString())
                && newPass.getText().toString() != null && !"".equals(newPass.getText().toString())
               ) {
            loinBut.setEnabled(true);
        }else {
            loinBut.setEnabled(false);
        }
    }

    @Override
    public void onRequestPermissions(String str, boolean z) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.close:
                finish();
                break;
            case R.id.loinBut:

                if (loinBut.isEnabled()) {
//                    Toast.makeText(this, "可以点击按钮了", Toast.LENGTH_SHORT).show();
                    // TODO 网络请求
                    change();
                }
                break;
        }
    }

    private void change(){
        String old = oldPass.getText().toString();
        String newP = newPass.getText().toString();
        if (TextUtils.isEmpty(old)){
            showToast("请输入原始密码");
            return;
        }

        if (TextUtils.isEmpty(newP)){
            showToast("请输入新密码");
            return;
        }

        String ss = SpUtils.getInstance().getString(this,"password","");
       String aa = Utlis.getMD5(old);
        aa = Utlis.getMD5(aa);
        if (!aa.equals(ss)){
            showToast("原始密码不正确");
            return;
        }

        LoadingDialog.showMassge(this,"修改中...");
        Map<String,String> map = new HashMap<>();
        String pass = Utlis.getMD5(newPass.getText().toString());
        pass = Utlis.getMD5(pass);
        map.put("password",pass);
        presenter.loadDataGetHeader(map,"", Apis.CHANGE_PASS,1001, Utlis.getHeadler(ChangePassActivity.this));


    }


    @Override
    public void returnData(String data, int urlCode) {


        if (TextUtils.isEmpty(data))
        {
            return;
        }
        handler.obtainMessage(urlCode,data).sendToTarget();
    }

    @Override
    public void LoadError(String errorMsg, int urlCode) {
        showToast("请求失败");
        LoadingDialog.hideProgress();
    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String str = (String) msg.obj;
            switch (msg.what){
                case 1001:
                    LoadingDialog.hideProgress();
                    if (TextUtils.isEmpty(str)){
                        return;
                    }

                    DataBean bean = Utlis.getDataBean(str);
                    showToast(bean.getMessage());
                    if (bean.getCode()==200){
                        MyApplication.getInstance().userInfo = null;
                        SpUtils.getInstance().save(ChangePassActivity.this,"token","");
                        MainActivity.mainActivity.token = "";
                        PersonInfoActivity.personInfoActivity.finish();
                        finish();
                    }
                    break;


            }

        }
    };
}
